package fr.thelem.gestiondemandesthelem.web.adminactions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import fr.thelem.gestiondemandesthelem.dao.model.AdministrateurEntity;
import fr.thelem.gestiondemandesthelem.dao.model.PersonneEntity;
import fr.thelem.gestiondemandesthelem.service.IAppliService;
import fr.thelem.gestiondemandesthelem.web.actions.BaseAction;

public class Emailer extends BaseAction {

	private IAppliService appliService;
    private String from = "";
    private String password = "";
    private String to = "";
    private String subject = "";
    private String body = "";
    private String email_recupere;
    private Map ContactUser = new HashMap();
    private String toEmails; 

    static Properties properties = new Properties();
    static {
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "465");
    }

    public String execute() {
        String ret = SUCCESS;
        recupererTousAdmins();
        try {
            Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, password);
                }
            });

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(body);
            
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails));
            
            
            Transport.send(message);
        } catch (Exception e) {
            ret = ERROR;
            e.printStackTrace();
        }
        return ret;
    }
    
    

    public IAppliService getAppliService() {
		return appliService;
	}



	public void setAppliService(IAppliService appliService) {
		this.appliService = appliService;
	}



	public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public static Properties getProperties() {
        return properties;
    }

    public static void setProperties(Properties properties) {
        Emailer.properties = properties;
    }

    public String getEmail_recupere() {
        return email_recupere;
    }

    public void setEmail_recupere(String email_recupere) {
        this.email_recupere = email_recupere;
    }

    public Map getContactUser() {
        return ContactUser;
    }

    public void setContactUser(Map contactUser) {
        ContactUser = contactUser;
    }
    

    public String getToEmails() {
		return toEmails;
	}

	public void setToEmails(String toEmails) {
		this.toEmails = toEmails;
	}

	@SuppressWarnings("null")
	public void recupererTousAdmins() {
		/*ArrayList<AdministrateurEntity> listeAdmins = (ArrayList<AdministrateurEntity>) this.appliService.recupererAdministrateurs();
		String[] auxLesAdmins = new String[listeAdmins.size()]; // initialise la taille du tableau
		this.toEmails = new String[listeAdmins.size()]; // initialise la taille du tableau
		
        for (int x=0; x < listeAdmins.size(); x++) {
        	auxLesAdmins[x] = listeAdmins.get(x).getAdresseMail();
        }*/
        
        setToEmails("D_Batch_launcher_Administrateur@thelem-assurances.fr");
	}
	
	public String ContacterUser() throws Exception {
        System.out.println(" contact user sans boucle " + this.from);
        recupererTousAdmins();
        for (Iterator i = ContactUser.keySet().iterator(); i.hasNext();) {
        	PersonneEntity person = appliService.recupererPersonneByLdap((String) i.next());
        	String email = person.getAdresseMail();
            setTo(email);
            
            System.out.println("avc boucle " + this.from);
        }

        return SUCCESS;
    }

}